/**
 * @file main.c
 * @author your name (you@domain.com)
 * @brief
 * @version 0.1
 * @date 2023-07-10
 *
 * @copyright Copyright (c) 2023
 *
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <FreeRTOS.h>
#include <task.h>
#include <queue.h>
#include "log.h"
#include "board.h"
#include "mem.h"
#include "bflb_gpio.h"
#include "ble_hid_dev.h"
//easy flash
#include "easyflash.h"
#include "bflb_mtd.h"
#include "bl_fw_api.h"
#include "wifi_mgmr_ext.h"
#include "wifi_mgmr.h"
#include "wifi_event.h"

//lvgl
#include "lv_conf.h"
#include "lvgl.h"
#include "lv_port_disp.h"
#include "lv_port_indev.h"
//GuiGuider UI
#include "gui_guider.h"
#include "custom.h"
#include "lv_user_config.h"

lv_ui guider_ui;
extern QueueHandle_t ble_hid_queue;
struct bflb_device_s* btn_gpio;
int btn_clicked = 0;
static void btn_event(void* args){
    while (1)
    {
        int status = bflb_gpio_read(btn_gpio, GPIO_PIN_17);
        if(status == 0){
            vTaskDelay(15/portTICK_RATE_MS);
            if(status == 0){
                if(btn_clicked){
                    continue;;
                }
                LOG_I("点击");
                btn_clicked = 1;
                hid_key_num_t hid_key_num = HID_KEY_NUMBLE_SELFIE_STICK;
                xQueueSend(ble_hid_queue, &hid_key_num, portMAX_DELAY);
            }
        }else{
            btn_clicked = 0;
        }
    }
    
}

int main(void)
{
    board_init();

    btn_gpio = bflb_device_get_by_name("gpio");
    bflb_gpio_init(btn_gpio, GPIO_PIN_17, GPIO_INPUT| GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
    

    tcpip_init(NULL, NULL);
    wifi_start_firmware_task();

    //init easyflash
    bflb_mtd_init();
    easyflash_init();
    st7789v_spi_init();
    /* lvgl init */
    lv_log_register_print_cb(lv_log_print_g_cb);
    lv_init();
    lv_port_disp_init();
    lv_port_indev_init();
    setup_ui(&guider_ui);
    custom_init(&guider_ui);
    events_init(&guider_ui);

    xTaskCreate(lvgl_tick_task, "lvgl", 1024, NULL, 1, NULL);
    xTaskCreate(btn_event, "btn_event", 1024, NULL, 1, NULL);
    xTaskCreate(ble_hid_task, "ble_hid_task", 1024, &guider_ui, 6, NULL);
    vTaskStartScheduler();

}